                
function showBuyNow() {
	dijit.byId('addToCart').domNode.style.display="inline";
	dijit.byId('buy').domNode.style.display="inline";
	dijit.byId('auction').domNode.style.display="none";
}

function showAuction() {
	dijit.byId('addToCart').domNode.style.display="none";
	dijit.byId("buy").domNode.style.display="none";
	dijit.byId('auction').domNode.style.display="inline";
}

/*Invoked when user clicks on 'Bid Now' button from auction item grid.*/
function bidNow(button){	
	var gridRowIndex = button.offsetParent.offsetParent.offsetParent.gridRowIndex;	
	var grid = dijit.byId("auctionGrid");
	grid.selection.clear();
	grid.selection.addToSelection(gridRowIndex);
	var selectedItem = 	grid.selection.getSelected();	
	var items = dijit.byId('auctionGrid').selection.getSelected();
	var selectedItems = new Array();
	
	if (items.length) {	
    	/* Iterate through the list of selected items.
        The current item is available in the variable
        "selectedItem" within the following function:*/
        dojo.forEach(items, function(selectedItem) {                    	
                            if (selectedItem !== null) {
                                /* Iterate through the list of attributes of each item.
                                 The current attribute is available in the variable
                                 "attribute" within the following function:*/
                                dojo.forEach(grid.store.getAttributes(selectedItem), function(attribute) {                                	
                                    // Get the value of the current attribute:
                                    if (attribute == "Id") {
                                    	var value = grid.store.getValues(selectedItem, attribute);                                    
                                    	selectedItems.push(value);                                  
                                    }
                                    /* Now, you can do something with this attribute/value pair.
                                     Our short example shows the attribute together
                                     with the value in an alert box, but we are sure, that
                                     you'll find a more ambitious usage in your own code:*/
                                    //alert('attribute: ' + attribute + ', value: ' + value);
                                }); // end forEach
                            } // end if
                        }); // end forEach
                    } // end if
	 var xhrArgs = {
                        url: "createBid.htm?itemId="+ selectedItems,                        
                        mimetype: "text/html",                        
                        content: {"selectedId": selectedItems,
        						  "selectedAuction" : dojo.byId('auctionList').value	                
		                        },
                        load: function(responseData) {
                        	//alert("Response Data:"+ 	responseData);     
                        	var dialog = dijit.byId('loadDiv');
							dialog.reset();
							dialog.titleNode.innerHTML = 'New Bid';                 
                        	dialog.attr("content", responseData);
                        	dialog.show();
                        },
                        error: function(error) {
                            /*We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
                            docs server.*/
                            dijit.byId("loadDiv").innerHTML = "Try Again. Unable to load popup.";
                            dijit.byId('loadDiv').show();
                        }
                    }
                    dojo.byId("response").innerHTML = "Sending to createBid..."
                    //Call the asynchronous xhrPost
                     var deferred = dojo.xhrPost(xhrArgs);	
}

function addToCart(){                 	
    var items = buyGrid.selection.getSelected();
    var selectedItems = new Array();
    if (items.length) {
        // Iterate through the list of selected items.
        // The current item is available in the variable
        // "selectedItem" within the following function:
        dojo.forEach(items, function(selectedItem) {
            if (selectedItem !== null) {
                // Iterate through the list of attributes of each item.
                // The current attribute is available in the variable
                // "attribute" within the following function:
                dojo.forEach(buyGrid.store.getAttributes(selectedItem), function(attribute) {                                	
                    // Get the value of the current attribute:
                    if (attribute == "Id") {
                    	var value = buyGrid.store.getValues(selectedItem, attribute);
                    	selectedItems.push(value);                                    
                    	
                    }
                    // Now, you can do something with this attribute/value pair.
                    // Our short example shows the attribute together
                    // with the value in an alert box, but we are sure, that
                    // you'll find a more ambitious usage in your own code:
                    //alert('attribute: ' + attribute + ', value: ' + value);
                }); // end forEach
            } // end if
        }); // end forEach
    } // end if                    
 	 var xhrArgs = {
        url: "addToCart.htm",                        
        mimetype: "text/json",                        
        content: {selectedIds: selectedItems},
        load: function(responseData) {                            
            var storeData = dojo.fromJson(responseData);                            
            dojo.byId("response").innerHTML = "Added to Cart." + responseData;
            try{                            	
            	var newStore = new dojo.data.ItemFileReadStore({data: storeData});                            
            	//cartGrid.store.data = storeData;
            	
            	cartGrid.setStore(newStore);
            	
                //cartStore.data = storeData;
                 //cartStore.close();
            }catch(e){
                 console.log(e);
            }
        },
        error: function(error) {
            //We'll 404 in the demo, but that's okay.  We don't have a 'postIt' service on the
            //docs server.
            dojo.byId("response").innerHTML = "Try Again. Unable to add to cart.";
        }
    }
    dojo.byId("response").innerHTML = "Adding to Cart..."
    //Call the asynchronous xhrPost
     var deferred = dojo.xhrPost(xhrArgs);
	
}

function postAuction() {	
	 var displayedValue =  dojo.byId("auctionList").value;
	 var displayedValue2 =  dojo.byId("auctionList").displayedValue;
	 var xhrArgs = {
                   url: "listAuctionItems.htm",                        
                   mimetype: "text/html",                        
                   content: {"selectedAuction" : displayedValue},
                     load: function(responseData) {
                       var storeData = dojo.fromJson(responseData);
                       dojo.byId("response").innerHTML = "Fetched auction items." + responseData;                            
                       try{                            	
                       	var newStore = new dojo.data.ItemFileReadStore({data: storeData});                            	                            	
                       	auctionGrid.setStore(newStore);
                       }catch(e){
                            console.log(e);
                       }
                   },
                   error: function(error) {
                       dojo.byId("response").innerHTML = "Try Again. Unable to add to cart.";
                   }
               }
               dojo.byId("response").innerHTML = "Fetched auction items."
               //Call the asynchronous xhrPost
                var deferred = dojo.xhrPost(xhrArgs);
}